package co.yixiang.modules.activity.service.impl;

import co.yixiang.modules.activity.domain.YxStoreCoupon;
import co.yixiang.modules.activity.repository.YxStoreCouponRepository;
import co.yixiang.modules.activity.service.YxStoreCouponService;
import co.yixiang.modules.activity.service.dto.YxStoreCouponDTO;
import co.yixiang.modules.activity.service.dto.YxStoreCouponQueryCriteria;
import co.yixiang.modules.activity.service.mapper.YxStoreCouponMapper;
import co.yixiang.modules.shop.domain.YxStoreProduct;
import co.yixiang.modules.shop.service.YxStoreProductService;
import co.yixiang.modules.shop.service.dto.YxStoreProductDTO;
import co.yixiang.modules.shop.service.mapper.YxStoreProductMapper;
import co.yixiang.utils.QueryHelp;
import co.yixiang.utils.StringUtils;
import co.yixiang.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author frq
 * @date 2020-08-05
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCouponServiceImpl implements YxStoreCouponService {

    private final YxStoreCouponRepository yxStoreCouponRepository;

    private final YxStoreCouponMapper yxStoreCouponMapper;

    private final YxStoreProductService yxStoreProductService;

    private final YxStoreProductMapper yxStoreProductMapper;

    public YxStoreCouponServiceImpl(YxStoreCouponRepository yxStoreCouponRepository, YxStoreCouponMapper yxStoreCouponMapper, YxStoreProductService yxStoreProductService, YxStoreProductMapper yxStoreProductMapper) {
        this.yxStoreCouponRepository = yxStoreCouponRepository;
        this.yxStoreCouponMapper = yxStoreCouponMapper;
        this.yxStoreProductService = yxStoreProductService;
        this.yxStoreProductMapper = yxStoreProductMapper;
    }

    @Override
    public Map<String, Object> queryAll(YxStoreCouponQueryCriteria criteria, Pageable pageable) {
        Page<YxStoreCoupon> page = yxStoreCouponRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        List<YxStoreCouponDTO> list = new ArrayList<>();
        for (YxStoreCoupon yxStoreCoupon : page.getContent()) {
            YxStoreCouponDTO dto=yxStoreCouponMapper.toDto(yxStoreCoupon);
            if (StringUtils.isNotEmpty(yxStoreCoupon.getProductId())) {
                List<YxStoreProduct> product = new ArrayList<>();
                String[] productIds = yxStoreCoupon.getProductId().split(",");
                for (String productId : productIds) {
                    YxStoreProductDTO yxStoreProductDTO = yxStoreProductService.findById(Integer.parseInt(productId));
                    if (yxStoreProductDTO != null) {
                        product.add(yxStoreProductMapper.toEntity(yxStoreProductDTO));
                    }
                }
                dto.setProduct(product);
            }
            list.add(dto);
        }
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", list);
        map.put("totalElements", page.getTotalElements());
        return map;
    }

    @Override
    public List<YxStoreCouponDTO> queryAll(YxStoreCouponQueryCriteria criteria) {
        return yxStoreCouponMapper.toDto(yxStoreCouponRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public YxStoreCouponDTO findById(Integer id) {
        Optional<YxStoreCoupon> yxStoreCoupon = yxStoreCouponRepository.findById(id);
        ValidationUtil.isNull(yxStoreCoupon, "YxStoreCoupon", "id", id);
        return yxStoreCouponMapper.toDto(yxStoreCoupon.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YxStoreCouponDTO create(YxStoreCoupon resources) {
        return yxStoreCouponMapper.toDto(yxStoreCouponRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreCoupon resources) {
        Optional<YxStoreCoupon> optionalYxStoreCoupon = yxStoreCouponRepository.findById(resources.getId());
        ValidationUtil.isNull(optionalYxStoreCoupon, "YxStoreCoupon", "id", resources.getId());
        YxStoreCoupon yxStoreCoupon = optionalYxStoreCoupon.get();
        yxStoreCoupon.copy(resources);
        yxStoreCouponRepository.save(yxStoreCoupon);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        yxStoreCouponRepository.deleteById(id);
    }
}